<?php

class Commodity
{
    /** @var int */
    protected $id = 0;
    /** @var string */
    protected $title = '';
    /** @var string */
    protected $intro = '';
    /** @var string */
    protected $content = '';
    /** @var int */
    protected $price = null;
    /** @var int */
    protected $stock = null;
    /** @var string */
    protected $contactType = null;
    /** @var string */
    protected $contactInfo = null;
    /** @var int */
    protected $releaseType = null;
    /** @var string */
    protected $releaseTime = '2000-01-01 00:00:00';
    /** @var string */
    protected $image_hash = null;
    /** @var int */
    protected $userId = 0;
    /** @var int */
    protected $commodityCatId = null;
    /** @var string */
    protected $commodityTagsIdJson = null;
    /** @var int */
    protected $schoolunicode = 0;

    /**
     * @return int
     */
    public function getSchoolunicode()
    {
        return $this->schoolunicode;
    }

    /**
     * @param int $schoolunicode
     */
    public function setSchoolunicode($schoolunicode)
    {
        $this->schoolunicode = $schoolunicode;
    }

    public function getDisplayPrice()
    {
        if ($this->getPrice() === null || !is_numeric($this->getPrice()))
            return '未设定';
        else if ($this->getPrice() == 0)
            return '留言我';
        return $this->getPrice() . ' 元';
    }

    public function getDisplayStock()
    {
        if ($this->getStock() === null || !is_numeric($this->getStock()))
            return '未设定';
        return $this->getStock() . ' 件';
    }

    public function getDisplayContactInfo()
    {
        if ($this->getContactInfo()) {
            return $this->getContactInfo();
        }
        return '请留言我';
    }

    public function getDisplayReleaseType()
    {
        if ($this->getReleaseType() === null)
            return '未设定';
        if ($this->getReleaseType() == 0)
            return '未指定';
        if ($this->getReleaseType() == 1)
            return '全新';
        if ($this->getReleaseType() == 2)
            return '二手';
        if ($this->getReleaseType() == 3)
            return '赠送';
        else
            return '[未知]';
    }

    public function getPlusOneCount()
    {
        return CommodityPlusOne_Model::countCommodityPlusOneByCommodityId($this->getId());
    }

    public function isCommodityPlusOneExistWithUserId($userId)
    {
        return CommodityPlusOne_Model::isCommodityPlusOneExist($this->getId(), $userId);
    }

    public function isCommodityFavoriteExistWithUserId($userId)
    {
        return CommodityFavorite_Model::isCommodityFavoriteExist($this->getId(), $userId);
    }


    /**
     * @return User
     */
    public function getUser()
    {
        return User_Model::getUserById($this->getUserId());
    }

    public function getUserDisplayName()
    {
        $thatUser = $this->getUser();
        if ($thatUser) {
            return $thatUser->getDisplayName();
        } else {
            return '[用户不存在]';
        }
    }

    public function getUserTrueName()
    {
        $thatUserId = $this->getUserId();
        if ($thatUserId && $thatUserId != null) {
            $thatUser = StudentInfo_Model::getStudentInfoByUserId($thatUserId);
            if ($thatUser != null) {
                return $thatUser->getStudentName();
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    public function getUserSex()
    {
        $thatUserId = $this->getUserId();
        if ($thatUserId && $thatUserId != null) {
            $thatUserInfo = UserInfo_Model::getUserInfoByUserId($thatUserId);
            if ($thatUserInfo != null) {
                return $thatUserInfo->getSex();
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    public function getDisplayImageUrl()
    {
        $thatFile = File_Model::getFileByHash($this->getImageHash());
        if ($thatFile)
            return base_url($thatFile->getUrl());

        $result = array();

        if (preg_match('/<img.*src=".*\/(.*?)\.(.*?)".*\/>/i', $this->getContent(), $result)) {
            if ($result && is_array($result) && isset($result[1]) && $result[1])
                $thatFile = File_Model::getFileByHash($result[1]);
            if ($thatFile)
                return base_url($thatFile->getUrl());
        }

        if (stristr($this->getContent(), '#OLD_CONTENT#')) {
            preg_match('/<img.+src=\"?(.+)\"?.+>/i', $this->getContent(), $result);
            if (isset($result[1]) && $result[1] !== null) {
                return $result[1];
            } else {
                return base_url('public/images/null_pic.png');
            }
        }
        return base_url('public/images/null_pic.png');
    }


    public function getDisplayImageHash()
    {
        $thatFile = File_Model::getFileByHash($this->getImageHash());
        if ($thatFile)
            return $this->getImageHash();

        $result = array();

        if (preg_match('/<img.*src=".*\/(.*?)\.(.*?)".*\/>/i', $this->getContent(), $result)) {
            if ($result && is_array($result) && isset($result[1]) && $result[1])
                $thatFile = File_Model::getFileByHash($result[1]);
            if ($thatFile)
                return $result[1];
        }

        return null;
    }

    public function getCommodityCatName()
    {
        $thatCommodityCategory = CommodityCategory_Model::getCommodityCategoryById($this->getCommodityCatId());
        if ($thatCommodityCategory)
            return $thatCommodityCategory->getName();
        else
            return '[分类不存在]';
    }

    public function getCommodityCommentCount()
    {
        return CommodityComment_Model::countCommodityCommentByCommodityId($this->getId());
    }

    /**
     * @return array
     */
    public function getDataArray()
    {
        return array(
            'id' => $this->getId(),
            'title' => $this->getTitle(),
            'intro' => $this->getIntro(),
            'content' => $this->getContent(),
            'price' => $this->getPrice(),
            'stock' => $this->getStock(),
            'contact_type' => $this->getContactType(),
            'contact_info' => $this->getContactInfo(),
            'releaseType' => $this->getReleaseType(),
            'releaseTime' => $this->getReleaseTime(),
            'userId' => $this->getUserId(),
            'commodityCatId' => $this->getCommodityCatId(),
            'commodityTagsIdJson' => $this->getCommodityTagsIdJson(),
            'schoolunicode' => $this->getSchoolunicode(),
        );
    }

    /**
     * @param int $commodityCatId
     */
    public function setCommodityCatId($commodityCatId)
    {
        $this->commodityCatId = $commodityCatId;
    }

    /**
     * @return int
     */
    public function getCommodityCatId()
    {
        return $this->commodityCatId;
    }

    /**
     * @return int
     */
    public function getCommodityCatFaId()
    {
        $commodityId = $this->getCommodityCatId();
        $commodityFa = CommodityCategory_Model::getCommodityCategoryById($commodityId);
        $commodityFaModel = CommodityCategory_Model::getModelByCommodityCategory($commodityFa);
        $commodityFaId = $commodityFaModel['father_id'];
        return $commodityFaId;
    }

    /**
     * @param string $commodityTagsIdJson
     */
    public function setCommodityTagsIdJson($commodityTagsIdJson)
    {
        $this->commodityTagsIdJson = $commodityTagsIdJson;
    }

    /**
     * @return string
     */
    public function getCommodityTagsIdJson()
    {
        return $this->commodityTagsIdJson;
    }

    /**
     * @param string $content
     */
    public function setContent($content)
    {
        $this->content = $content;
    }

    /**
     * @return string
     */
    public function getContent()
    {
        return $this->content;
    }

    /**
     * @param int $id
     */
    public function setId($id)
    {
        $this->id = $id;
    }

    /**
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @param int $price
     */
    public function setPrice($price)
    {
        $this->price = $price;
    }

    /**
     * @return int
     */
    public function getPrice()
    {
        return $this->price;
    }

    /**
     * @param string $releaseTime
     */
    public function setReleaseTime($releaseTime)
    {
        $this->releaseTime = $releaseTime;
    }

    /**
     * @return string
     */
    public function getReleaseTime()
    {
        return $this->releaseTime;
    }

    /**
     * @param int $releaseType
     */
    public function setReleaseType($releaseType)
    {
        $this->releaseType = $releaseType;
    }

    /**
     * @return int
     */
    public function getReleaseType()
    {
        return $this->releaseType;
    }

    /**
     * @param int $stock
     */
    public function setStock($stock)
    {
        $this->stock = $stock;
    }

    /**
     * @return int
     */
    public function getStock()
    {
        return $this->stock;
    }

    /**
     * @param string $title
     */
    public function setTitle($title)
    {
        $this->title = $title;
    }

    /**
     * @return string
     */
    public function getTitle()
    {
        return $this->title;
    }

    /**
     * @param int $userId
     */
    public function setUserId($userId)
    {
        $this->userId = $userId;
    }

    /**
     * @return int
     */
    public function getUserId()
    {
        return $this->userId;
    }

    /**
     * @return string
     */
    public function getImageHash()
    {
        return $this->image_hash;
    }

    /**
     * @param string $image_hash
     */
    public function setImageHash($image_hash)
    {
        $this->image_hash = $image_hash;
    }

    /**
     * @return string
     */
    public function getContactType()
    {
        return $this->contactType;
    }

    /**
     * @param string $contactType
     */
    public function setContactType($contactType)
    {
        $this->contactType = $contactType;
    }

    /**
     * @return string
     */
    public function getContactInfo()
    {
        return $this->contactInfo;
    }

    /**
     * @param string $contactInfo
     */
    public function setContactInfo($contactInfo)
    {
        $this->contactInfo = $contactInfo;
    }

    /**
     * @return string
     */
    public function getIntro()
    {
        return $this->intro;
    }

    /**
     * @param string $intro
     */
    public function setIntro($intro)
    {
        $this->intro = $intro;
    }

}